package com.sinoatma.physignsmonitor

import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.clj.fastble.BleManager
import com.clj.fastble.scan.BleScanRuleConfig
import com.common.frame.utils.Utils
import com.scandecode.inf.ScanInterface
import com.sinoatma.physignsmonitor.bean.AppDatabase
import com.sinoatma.physignsmonitor.bean.MonitorRecordDao
import com.sinoatma.physignsmonitor.ble.BleBloodTransService
import com.sinoatma.physignsmonitor.ble.BleStartService
import com.sinoatma.physignsmonitor.ble.BleStartServiceTwo
import com.sinoatma.physignsmonitor.constant.BloodTransfusionModeFlag
import com.sinoatma.physignsmonitor.constant.devicesNumber
import com.sinoatma.physignsmonitor.utils.ExcelServiceImpl
import java.nio.ByteBuffer
import java.nio.ByteOrder


/**
 * @ProjectName : BloodCollectionChair
 * @Author :-
 * @Time : 2023/3/10 11:29
 * @Description : 文件描述
 */
class MyApp:Application() {
    companion object{
        lateinit var context: Context
            private set
        lateinit var monitorRecordDao: MonitorRecordDao
        lateinit var scanDecode: ScanInterface
        lateinit var bluetoothConnectService: Intent
        lateinit var bluetoothConnectServiceTwo: Intent
        lateinit var bleBloodTransService: Intent
        lateinit var excelService:ExcelServiceImpl
        var stopMonitorFlag1=0
        var stopMonitorFlag2=0
    }
    override fun onCreate() {
        super.onCreate()
        context = applicationContext
        Utils.init(this)
        monitorRecordDao=AppDatabase.getDatabase(context).monitorRecordDao()
        excelService = ExcelServiceImpl()
        if (BloodTransfusionModeFlag ==1){
            bluetoothConnectService = Intent(this, BleStartService::class.java)
            bluetoothConnectServiceTwo= Intent(this, BleStartServiceTwo::class.java)
        }else{
            bleBloodTransService= Intent(this, BleBloodTransService::class.java)
        }

       if (BloodTransfusionModeFlag==1){
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               startForegroundService(bluetoothConnectService)
               startForegroundService(bluetoothConnectServiceTwo)
           } else {
               startService(bluetoothConnectService)
               startService(bluetoothConnectServiceTwo)
           }
       }else{
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               startForegroundService(bleBloodTransService)
           } else {
               startService(bleBloodTransService)
           }
       }
        BleManager.getInstance().init(this)
        BleManager.getInstance()
            .enableLog(true)
            .setReConnectCount(1, 5000)
            .setSplitWriteNum(20)
            .setConnectOverTime(10000)
            .operateTimeout = 10000
        val scanRuleConfig = BleScanRuleConfig.Builder()
            .setAutoConnect(true)
            .setScanTimeOut(20000)
            .build()
        BleManager.getInstance().initScanRule(scanRuleConfig)
    }
}